package com.connector.base.message.model;

import com.alibaba.fastjson.JSONObject;
import com.connector.base.connection.Connection;
import com.connector.base.protocol.Packet;
import com.connector.base.utils.ErrorCode;
import io.netty.channel.ChannelFutureListener;

public final class ErrorMessage extends JsonBodyMessage
{
    public String type;
    public String data;

    private ErrorMessage setData(final String data) {
        this.type = "0";
        this.data = data;
        return this;
    }

    public ErrorMessage setErrorCode(final ErrorCode code) {
        final JSONObject dataObj = new JSONObject();
        dataObj.put("success", (Object)false);
        dataObj.put("msg", (Object)code.errorMsg);
        return this.setData(dataObj.toJSONString());
    }

    public ErrorMessage(final Packet packet, final Connection connection) {
        super(packet, connection);
    }

    @Override
    public void jsonToFields(final JSONObject jsonBody) {
        this.type = (jsonBody.containsKey((Object)"type") ? jsonBody.getString("type") : null);
        this.data = (jsonBody.containsKey((Object)"data") ? jsonBody.getString("data") : null);
    }

    @Override
    public JSONObject fieldToJson() {
        final JSONObject resultObj = new JSONObject();
        resultObj.put("type", (Object)this.type);
        resultObj.put("data", (Object)this.data);
        return resultObj;
    }

    public ErrorMessage buildResult(final String code, final String data) {
        final JSONObject dataObj = new JSONObject();
        dataObj.put("success", (Object)false);
        dataObj.put("msg", (Object)(code + data));
        return this.setData(dataObj.toJSONString());
    }

    @Override
    public void channelSend(final ChannelFutureListener listener) {
        super.channelSend(listener);
    }

    @Override
    public void close() {
        super.close();
    }

    public String getType() {
        return this.type;
    }

    public String getData() {
        return this.data;
    }

    @Override
    public String toString() {
        return "ErrorMessage(type=" + this.getType() + ", data=" + this.getData() + ")";
    }

    public ErrorMessage() {
    }
}
